package com.nhjf.dao.jobDetails;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.jobDetails.JobDetails;
import com.nhjf.pojo.qo.jobDetails.JobDetailsQo;

@Repository
public class JobDetailsDao extends BaseDao<JobDetails, JobDetailsQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, JobDetailsQo qo) {
		// TODO Auto-generated method stub
		if(qo!=null) {
			criteria.setFetchMode("postJob", qo.isSeachPostJob()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("student", qo.isSeachStudent()?FetchMode.JOIN:FetchMode.SELECT);	
			if(StringUtils.isNotBlank(qo.getChangepaintingURL())) {
				criteria.add(Restrictions.eq("changepaintingURL",qo.getChangepaintingURL()));
			}
			if(StringUtils.isNotBlank(qo.getNotchangedURL())) {
				criteria.add(Restrictions.eq("notchangedURL",qo.getNotchangedURL()));
			}
			if(qo.getJobDetailsId()!=null) {
				criteria.add(Restrictions.eq("id",qo.getJobDetailsId()));
			}
			if(qo.getPostJobId()!=null) {
				criteria.add(Restrictions.eq("postJob.id",qo.getPostJobId()));
			}
			if(qo.getStudentId()!=null) {
				criteria.add(Restrictions.eq("student.id",qo.getStudentId()));
			}
			if(qo.getReleasetime()!=null) {
				criteria.add(Restrictions.eq("releasetime",qo.getReleasetime()));
			}
			if(qo.getReviewtime()!=null) {
				criteria.add(Restrictions.eq("reviewtime",qo.getReviewtime()));
			}
			if(qo.getStatus()!=null) {
				criteria.add(Restrictions.eq("status",qo.getStatus()));
			}
			if(qo.isSeachTypeOne()) {
				criteria.createAlias("postJob", "postJob");
				criteria.createAlias("postJob.typeOne", "uploader.typeOne");
			}
			//未交作业原因：1旷课、2请假、3已补交
			if(qo.getUnpaidReason()!=null) {
				criteria.add(Restrictions.eq("unpaidReason",qo.getUnpaidReason()));
			}
			if(qo.getSccore()!=null) {
				criteria.add(Restrictions.eq("sccore",qo.getSccore()));
			}
			  criteria.addOrder(Order.desc("creaTime"));
		}
		return criteria;
	}

	@Override
	protected Class<JobDetails> getEntityClass() {
		// TODO Auto-generated method stub
		return JobDetails.class;
	}

}
